import FingerprintJS from '@fingerprintjs/fingerprintjs';
import { useCache } from '@/hooks/web/useCache'

const TEMP_FINGERPRINT = 'fingerprint';


export function setFingerprint(fingerprint: string) {
    const { wsCache } = useCache()
    // 设置token
    wsCache.set(TEMP_FINGERPRINT, fingerprint)
  }

export async function getFingerprint() {
  const { wsCache } = useCache()
  let fingerprintVal = await wsCache.get(TEMP_FINGERPRINT);
  if (!fingerprintVal) {
    // 重新生成
    const fp = await FingerprintJS.load();
    const result = await fp.get();
    const visitorId = result.visitorId;
    if (visitorId) {
        setFingerprint(visitorId);
        fingerprintVal = visitorId;
    }
  }
  return fingerprintVal;
}

